package com.magictower.primary.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;

import com.magictower.primary.R;
import com.magictower.primary.common.SPConstant;
import com.magictower.primary.service.LocationService;

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] objects = (Object[]) intent.getExtras().get("pdus");
        for (Object object : objects) {
            // 短信最多140个字节，多于140字节则分成多条短信
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) object);

            String originatingAddress = smsMessage.getOriginatingAddress();//短信来源号码
            String messageBody = smsMessage.getMessageBody();//短信内容

            if("#*alarm*#".equals(messageBody)) {//警报
                // 播放报警音乐, 即使手机调为静音,也能播放音乐, 因为使用的是媒体声音的通道,和铃声无关
                MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.ylzs);

                mediaPlayer.setVolume(1F, 1F);
                mediaPlayer.setLooping(true);
                mediaPlayer.start();

                abortBroadcast();//中断广播

            } else if("#*location*#".equals(messageBody)) {//定位
                // 开启服务获取位置
                context.startService(new Intent(context,LocationService.class));
                SharedPreferences mPref = context.getSharedPreferences(SPConstant.CONFIG, Context.MODE_PRIVATE);
                String locationStr = mPref.getString(SPConstant.LOCATION, "getting location...");

                //返回传递经纬度信息给安全号码
                System.out.println("经纬度：" + locationStr);

                abortBroadcast();

            } else if("#*wipedata*#".equals(messageBody)) {//擦除信息

            } else if("#*lockscreen*#".equals(messageBody)) {//锁屏

            }
        }
    }
}
